#include <signal.h>

#include "../comm/httplib.h"
#include "oj_control.hpp"
using namespace httplib;
using namespace ns_contrl;

Control ctrl;

void Recovery(int signo)
{
    ctrl.RecoveryMachines();
}
int main()
{
    signal(SIGQUIT,Recovery);
    // 用户请求的路由功能
    Server svr;
    svr.Get("/all_questions", [](const Request &req, Response &resp)
                { 
                    std::string html;
                    ctrl.AllQuestions(&html);
                    resp.set_content(html,"text/html;charset=utf-8");
                }
            );

    // 用户要根据题目标号，获取题目的内容
    // /question/100 ->正则匹配   R"()",原始字符串raw string,保持字符串内容的原貌，不用转义
    svr.Get(R"(/question/(\d+))", [](const Request &req, Response &resp)
                {
                    std::string number = req.matches[1];
                    std::string html;
                    ctrl.OneQuestion(number,&html);
                    resp.set_content(html,"text/html;charset=utf-8");
                }
            );

    // 用户提交代码
    svr.Post(R"(/judge/(\d+))", [](const Request &req, Response &resp)
            {
                std::string number = req.matches[1];
                std::string result_json;
                ctrl.Judge(number,req.body,&result_json);
                resp.set_content(result_json,"application/json;charset=utf-8");
            });
    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8080);
    return 0;
}